home *** CD-ROM | disk | FTP | other *** search
- /*
- File: ODStor.cpp
-
- Contains: Implementation of ODStorageSystem
-
- Owned by: Vincent Lo
-
- Copyright: © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <6> 5/24/96 jpa 1246074: SOM_CATCH --> SOM_TRY..SOM_ENDTRY
- <5> 5/1/96 JA 1314704: Directly instantiate Bento classes
- w/o going through ODNewObject.
- <4> 4/23/96 TJ Removed ODStorB.cpp
- <2> 3/15/96 DM 1295410: create list iterators on stack
- (avoid mem thrash during purge)
-
- To Do:
- **** ERROR HANDLING!!!! ****
- In Progress:
-
- */
-
- #define ODStorageSystem_Class_Source
- #define VARIABLE_MACROS
- #include <ODStor.xih>
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef _STORPRIV_
- #include "StorPriv.h"
- #endif
-
- #ifndef SOM_ODPart_xh
- #include <Part.xh>
- #endif
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include <NmSpcMg.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdDefs_defined
- #include <StdDefs.xh>
- #endif
-
- #ifndef SOM_ODTranslation_xh
- #include <Translt.xh>
- #endif
-
- #ifndef _ISOSTR_
- #include "ISOStr.h"
- #endif
-
- #ifndef SOM_ODBinding_xh
- #include <ODBindng.xh>
- #endif
-
- #ifndef SOM_ODTypeList_xh
- #include <TypeList.xh>
- #endif
-
- #ifndef SOM_ODPlatformTypeList_xh
- #include <PfTypLs.xh>
- #endif
-
- #ifndef SOM_ODFileContainer_xh
- #include "FileCtr.xh"
- #endif
-
- #ifndef SOM_ODEmbeddedContainer_xh
- #include "EmbedCtr.xh"
- #endif
-
- #ifndef SOM_ODMemContainer_xh
- #include "MemCtr.xh"
- #endif
-
- #ifndef _ODNEW_
- #include <ODNew.h>
- #endif
-
- #ifndef _ODMemory_
- #include <ODMemory.h>
- #endif
-
- #ifndef _UTILERRS_
- #include "UtilErrs.h"
- #endif
-
- //#define STATICBENTO
- #ifndef STATICBENTO
-
- #ifndef _ODNEWOBJ_
- #include "ODNewObj.h"
- #endif
-
- #ifndef __STRING__
- #include <string.h> // Only for comparing ContainerType. Should be removed when we use DLL.
- #endif
-
- #else
-
- #ifndef SOM_ODFileContainer_xh
- #include <FileCtr.xh>
- #endif
-
- #ifndef SOM_ODEmbeddedContainer_xh
- #include <EmbedCtr.xh>
- #endif
-
- #ifndef SOM_ODMemContainer_xh
- #include <MemCtr.xh>
- #endif
-
- #endif
-
- //==============================================================================
- // Constants
- //==============================================================================
-
-
- //==============================================================================
- // Function Prototype
- //==============================================================================
-
- #ifdef STATICBENTO
-
- static ODFileContainer* NewODFileContainer(ODMemoryHeapID heapID);
- static ODMemContainer* NewODMemContainer(ODMemoryHeapID heapID);
- static ODEmbeddedContainer* NewODEmbeddedContainer(ODMemoryHeapID heapID);
-
- #endif
-
- #pragma segment ODStorageSystem
-
- //#include "ODStorB.cpp" // Platform-independent methods, if any
-
- SOM_Scope ODMemoryHeapID SOMLINK ODStorageSystemGetHeap(ODStorageSystem *somSelf, Environment *ev)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","GetHeap");
-
- return _fHeap;
- }
-
- SOM_Scope void SOMLINK ODStorageSystemsomUninit(ODStorageSystem *somSelf)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemsomUninit");
-
- if (_fContainers)
- {
- TRY{
- Environment* ev = somGetGlobalEnvironment();
-
- ContainerListIterator iter(_fContainers);
- iter.Initialize();
-
- ODContainer* container = iter.Last();
- while (container != kODNULL) {
- container->Close(ev);
- delete container;
- container = iter.Previous();
- }
- // delete iter;
-
- delete _fContainers;
- }CATCH_ALL{
- }ENDTRY
- }
- }
-
- SOM_Scope ODSize SOMLINK ODStorageSystemPurge(ODStorageSystem *somSelf, Environment *ev,
- ODSize size)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","Purge");
-
- ODULong sizePurged = 0; ODVolatile( sizePurged );
-
- SOM_TRY
-
- ContainerListIterator iter(_fContainers);
- iter.Initialize();
-
- ODContainer* container = iter.Last();
- while (container != kODNULL) {
- TRY
- sizePurged += container->Purge(ev, size-sizePurged);
- CATCH_ALL
- ENDTRY
- // dh - I did't wrap the iterator call in a TRY block, because there is no
- // use continuing with the iterator if there is a problem with it.
- container = iter.Previous();
- }
- // delete iter;
-
- sizePurged += parent_Purge(somSelf,ev,size);
- SOM_CATCH_ALL
- WARN("Error %ld trying to purge in ODStorageSystemPurge",ErrorCode());
- SetErrorCode(kODNoError); // dh - Eat the exception; Purge should not
- // propagate it because clients function
- // fine whether memory was purged or not.
- SOM_ENDTRY
-
- return sizePurged;
- }
-
- SOM_Scope void SOMLINK ODStorageSystemInitStorageSystem(ODStorageSystem *somSelf, Environment *ev,
- ODSession* session)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","InitStorageSystem");
-
- SOM_TRY
-
- /* Moved from somInit. SOM itself sets fields to zero
- _fSession = kODNULL;
- _fHeap = kDefaultHeapID;
- _fContainers = kODNULL;
- */
- _fSession = session;
- _fHeap = ODRecoverHeapID(somSelf);
-
- _fContainers = new(somSelf->GetHeap(ev)) ContainerList;
- _fContainers->Initialize();
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- SOM_Scope ODSession* SOMLINK ODStorageSystemGetSession(ODStorageSystem *somSelf, Environment *ev)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemGetSession");
-
- return _fSession;
- }
-
- SOM_Scope ODContainer* SOMLINK ODStorageSystemAcquireContainer(ODStorageSystem *somSelf, Environment *ev,
- ODContainerType containerType,
- ODContainerID* id)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemAcquireContainer");
-
- SOM_TRY
-
- ODContainer* container = kODNULL;
-
- if ((container = _fContainers->Get(id)) != kODNULL) {
- container->Acquire(ev);
- }
- else {
- #ifndef STATICBENTO
-
- ODContainerSuite theContainerSuite =
- _fSession->GetBinding(ev)->GetContainerSuite(ev,containerType);
-
- if ( theContainerSuite )
- {
- TRY
- container = (ODContainer*) ODNewObject(theContainerSuite);
- CATCH_ALL
- ODDisposePtr( theContainerSuite );
- THROW( ErrorCode()==kODErrCantLoadSOMClass ?kODErrCannotCreateContainer
- :ErrorCode() );
- ENDTRY
- ODDisposePtr( theContainerSuite );
- }
- else
- THROW( kODErrCannotCreateContainer );
-
- #else
- // Hardwire for ODContainer for now. We should do the dynamic loading
- // of the class using containerType in the future.
-
- if (strcmp(kODDefaultFileContainer, containerType) == 0)
- container = NewODFileContainer(somSelf->GetHeap(ev));
- else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
- container = NewODEmbeddedContainer(somSelf->GetHeap(ev));
- else if (strcmp(kODDefaultMemoryContainer, containerType) == 0)
- container = NewODMemContainer(somSelf->GetHeap(ev));
-
- #endif
- container->InitContainer(ev, somSelf, id);
-
- _fContainers->Add(container);
-
- container->Open(ev);
- }
- return container;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return kODNULL;
- }
-
- SOM_Scope ODContainer* SOMLINK ODStorageSystemCreateContainer(ODStorageSystem *somSelf, Environment *ev,
- ODContainerType containerType,
- ODContainerID* id)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemCreateContainer");
-
- SOM_TRY
-
- ODContainer* container = kODNULL;
-
- if ((container = _fContainers->Get(id)) != kODNULL)
- THROW(kODErrContainerExists);
-
- #ifndef STATICBENTO
-
- // Hardwire the Bento classnames for speed.
- if (strcmp(kODBentoFileContainer, containerType) == 0)
- container = new ODFileContainer;
- else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
- container = new ODEmbeddedContainer;
- else if (strcmp(kODBentoMemoryContainer, containerType) == 0)
- container = new ODMemContainer;
- else {
-
- // General case:
- ODContainerSuite theContainerSuite =
- _fSession->GetBinding(ev)->GetContainerSuite(ev,containerType);
-
- if ( theContainerSuite )
- {
- TRY
- container = (ODContainer*) ODNewObject(theContainerSuite);
- CATCH_ALL
- ODDisposePtr( theContainerSuite );
- THROW( ErrorCode()==kODErrCantLoadSOMClass ?kODErrCannotCreateContainer
- :ErrorCode() );
- ENDTRY
- ODDisposePtr( theContainerSuite );
- }
- else
- THROW( kODErrCannotCreateContainer );
- }
-
- if( !container )
- THROW( kODErrCannotCreateContainer );
-
- #else
- // Hardwire for the two Bento Containers for now. We should do the dynamic loading
- // of the class using containerType in the future.
-
- if (strcmp(kODDefaultFileContainer, containerType) == 0)
- container = NewODFileContainer(somSelf->GetHeap(ev));
- else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
- container = NewODEmbeddedContainer(somSelf->GetHeap(ev));
- else if (strcmp(kODDefaultMemoryContainer, containerType) == 0)
- container = NewODMemContainer(somSelf->GetHeap(ev));
-
- #endif
-
- container->InitContainer(ev, somSelf, id);
-
- _fContainers->Add(container);
-
- container->Create(ev);
- container->Open(ev);
-
- return container;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return kODNULL;
- }
-
- SOM_Scope void SOMLINK ODStorageSystemNeedSpace(ODStorageSystem *somSelf, Environment *ev,
- ODSize memSize,
- ODBoolean doPurge)
- {
- //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemNeedSpace");
-
- if (doPurge != kODFalse) {
- SOM_TRY
- somSelf->Purge(ev, memSize);
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
- }
-
- SOM_Scope ODTypeList* SOMLINK ODStorageSystemCreateTypeList(ODStorageSystem *somSelf, Environment *ev,
- ODTypeList* typeList)
- {
- //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemCreateTypeList");
-
- ODTypeList* tmp; ODVolatile(tmp);
-
- SOM_TRY
-
- tmp = new ODTypeList;
- THROW_IF_NULL(tmp);
- tmp->InitTypeList(ev, typeList);
-
- SOM_CATCH_ALL
- ODDeleteObject(tmp);
- SOM_ENDTRY
- return tmp;
- }
-
- SOM_Scope ODPlatformTypeList* SOMLINK ODStorageSystemCreatePlatformTypeList(ODStorageSystem *somSelf, Environment *ev,
- ODPlatformTypeList* typeList)
- {
- //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemNeedSpace");
-
- ODPlatformTypeList* tmp;
-
- SOM_TRY
-
- tmp = new ODPlatformTypeList;
- if (tmp == kODNULL)
- ODSetSOMException(ev, kODErrOutOfMemory);
- else
- tmp->InitPlatformTypeList(ev, typeList);
-
- SOM_CATCH_ALL
- tmp = kODNULL;
- SOM_ENDTRY
-
-
- return tmp;
- }
-
-
- SOM_Scope void SOMLINK ODStorageSystemGrowHeap(ODStorageSystem *somSelf, Environment *ev,
- ODSize memSize)
- {
- //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemGrowHeap");
-
- ODUnused(memSize);
- }
-
- SOM_Scope ODStorageSystem* SOMLINK ODStorageSystemReleaseContainer(ODStorageSystem *somSelf, Environment *ev,
- ODContainer* container)
- {
- ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
- ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemReleaseContainer");
-
- SOM_TRY
-
- ODContainerID containerID = container->GetID(ev);
-
- if ((container = _fContainers->Get(&containerID)) != kODNULL) {
- _fContainers->Remove(&containerID);
- ODDisposePtr(containerID._buffer);
- TRY
- container->Close(ev);
- delete container;
- CATCH_ALL
- SetErrorCode(kODNoError);
- ENDTRY
- }
- else
- {
- ODDisposePtr(containerID._buffer);
- THROW(kODErrContainerDoesNotExist);
- }
-
- return somSelf;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return somSelf;
- }
-
- #ifdef STATICBENTO
-
- //------------------------------------------------------------------------------
- // NewODFileContainer
- //------------------------------------------------------------------------------
-
- static ODFileContainer* NewODFileContainer(ODMemoryHeapID heapID)
- {
- SOMClass* fileCtrClass = ODFileContainerNewClass(ODFileContainer_MajorVersion,
- ODFileContainer_MinorVersion);
- ODULong size = fileCtrClass->somGetInstanceSize();
- ODPtr buffer = ODNewPtr(size, heapID);
- ODFileContainer* fileCtr = (ODFileContainer*) fileCtrClass->somRenew(buffer);
-
- return fileCtr;
- }
-
- //------------------------------------------------------------------------------
- // NewODMemContainer
- //------------------------------------------------------------------------------
-
- static ODMemContainer* NewODMemContainer(ODMemoryHeapID heapID)
- {
- SOMClass* memCtrClass = ODMemContainerNewClass(ODMemContainer_MajorVersion,
- ODMemContainer_MinorVersion);
- ODULong size = memCtrClass->somGetInstanceSize();
- ODPtr buffer = ODNewPtr(size, heapID);
- ODMemContainer* memCtr = (ODMemContainer*) memCtrClass->somRenew(buffer);
-
- return memCtr;
- }
-
- //------------------------------------------------------------------------------
- // NewODEmbeddedContainer
- //------------------------------------------------------------------------------
-
- static ODEmbeddedContainer* NewODEmbeddedContainer(ODMemoryHeapID heapID)
- {
- SOMClass* embeddedCtrClass = ODEmbeddedContainerNewClass(ODEmbeddedContainer_MajorVersion,
- ODEmbeddedContainer_MinorVersion);
- ODULong size = embeddedCtrClass->somGetInstanceSize();
- ODPtr buffer = ODNewPtr(size, heapID);
- ODEmbeddedContainer* embeddedCtr = (ODEmbeddedContainer*) embeddedCtrClass->somRenew(buffer);
-
- return embeddedCtr;
- }
-
- #endif
-
-
-
-